#include <stdio.h>
#include <stdlib.h>

#define ROWS 2
#define COLS 2

int main() {
    // 定义一个 n*n 的二进制矩阵
    unsigned char binaryMatrix[ROWS][COLS] = {
        {3, 0},
        {0, 2},
    };

    // 打开文件以二进制写入模式
    FILE *file = fopen("sparse_mat.bin", "wb");
    if (file == NULL) {
        perror("Error opening file");
        return EXIT_FAILURE;
    }


    // 写入矩阵的行数和列数
    fwrite(ROWS, sizeof(int), 1, file);
    fwrite(COLS, sizeof(int), 1, file);

    // 写入矩阵数据
    size_t matrixSize = ROWS * COLS;
    fwrite(binaryMatrix, sizeof(unsigned char), matrixSize, file);

    // 关闭文件
    fclose(file);

    printf("Matrix has been saved to sparse_mat.bin\n");

    return EXIT_SUCCESS;
            // 打印矩阵
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            printf("%d ", binaryMatrix[i][j]);
        }
        printf("\n"); // 每行结束后换行
    }
}